package com.docker.manege.server;

import com.alibaba.fastjson.JSONObject;
import com.docker.comment.ShellUtil;
import com.docker.core.mvc.service.BaseService;
import com.docker.core.third.docker.DockerKit;
import com.docker.core.third.websocket.WebsocketCMD;
import com.docker.core.third.websocket.WebsocketPool;
import com.jfinal.aop.Inject;
import com.jfinal.kit.Okv;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import kit.docker.convert.RecordKit;

import java.util.List;

/**
 * @Author ldl
 * @Create 2020/12/02 002 14:54
 */
public class DockerServerService extends BaseService {

    @Inject
    private DockerServerDao dockerServerDao;

    public Page<Record> paginate(Okv cond, int pageNum, int pageSize) {
        return dockerServerDao.paginate(cond, pageNum, pageSize);
    }

    public List<Record> look(Okv cond) {
        return dockerServerDao.look(cond);
    }

    public boolean save(DockerServerBean bean, String userId) {
        bean.setServer_id(getUUID32());
        bean.setStatus_flag("1");
        bean.setRemark("新增docker服务信息");
        bean.setCreate_time(getNowDateTime());
        bean.setCreate_oper_id(userId);
        bean.setUpdate_time(bean.getCreate_time());
        bean.setUpdate_oper_id(bean.getCreate_oper_id());
        if (!dockerServerDao.save(RecordKit.objectToRecord(bean))) {
            throwError("新增docker服务信息失败");
        }
        return true;
    }

    public Record info(String serverId) {
        Record info = dockerServerDao.info(serverId);
        if (!StrKit.notNull(info)) {
            throwError("服务器配置信息不存在");
        }
        if (!"1".equals(info.getStr("status_flag"))) {
            throwError("服务器配置信息已删除");
        }
        return info;
    }

    public Record detail(String serverId) {
        return dockerServerDao.detail(serverId);
    }

    public boolean update(DockerServerBean bean, String userId) {
        Record info = info(bean.getServer_id());
        info.set("docker_name", bean.getDocker_name());
        info.set("docker_host", bean.getDocker_host());
        info.set("docker_port", bean.getDocker_port());
        info.set("cert_path", bean.getCert_path());
        info.set("config_path", bean.getConfig_path());
        info.set("api_version", bean.getApi_version());
        info.set("registry_url", bean.getRegistry_url());
        info.set("user_name", bean.getUser_name());
        info.set("password", bean.getPassword());
        info.set("email", bean.getEmail());
        info.set("max_connect", bean.getMax_connect());
        info.set("logger_server_port", bean.getLogger_server_port());
        info.set("note", bean.getNote());
        info.set("remark", "修改docker服务信息");
        info.set("update_time", getNowDateTime());
        info.set("update_oper_id", userId);
        if (!dockerServerDao.update(info)) {
            throwError("修改docker服务信息失败");
        }
        return true;
    }

    public boolean delete(String serverId, String userId) {
        Record info = info(serverId);
        info.set("status_flag", "0");
        info.set("remark", "删除docker服务信息");
        info.set("update_time", getNowDateTime());
        info.set("update_oper_id", userId);
        if (!dockerServerDao.update(info)) {
            throwError("删除docker服务信息失败");
        }
        return true;
    }

    public boolean connect(String token, String serverId) {
        Record docker = info(serverId);
        String OS = System.getProperty("os.name").toLowerCase();
        if (OS.indexOf("windows")>=0){
            docker.set("cert_path",docker.getStr("cert_path_window"));
            docker.set("config_path",docker.getStr("config_path_window"));
        }
        if (!DockerKit.connect(getClientKey(token, serverId), docker)) {
            throwError("docker连接失败");
        }
        return true;
    }

    public boolean disconnect(String token, String serverId) {
        return DockerKit.colse(getClientKey(token, serverId));
    }

    public void onMessage(WebsocketPool socketPool, JSONObject msgJson) {
        switch (msgJson.getString("cmd")) {
            case WebsocketCMD.ON_MESSAGE:
                ShellUtil.transToSSH(socketPool.getConnect_key(),msgJson.getJSONObject("data").getString("data"));
                break;
            default:
                //to do some thing
                break;
        }
    }
}
